package com.ttc.mBackup.BackupImages;

import java.io.File;

import com.ttc.mBackup.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class RunRestoreActivity extends Activity {

	private TextView txtFileName;
	private TextView txtDate;
	private TextView txtDescriptions;

	private TextView txtStatus;
	private TextView txtPercent;
	private ProgressBar progressBar;
	private int restoreType = RestoreImages.BACKUP_MERGE;
	private RestoreImages restoreTask;
	private String backupPath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.i_layout_run_restore);

		txtFileName = (TextView) findViewById(R.id.i_run_restore_filename);
		txtDate = (TextView) findViewById(R.id.i_run_restore_date);
		txtDescriptions = (TextView) findViewById(R.id.i_run_restore_description);
		txtStatus = (TextView) findViewById(R.id.i_restore_txtStatus);
		txtPercent = (TextView) findViewById(R.id.i_restore_txtPercent);
		progressBar = (ProgressBar) findViewById(R.id.i_progress_restore);
		restoreTask = null;
		try {
			Bundle bundle = getIntent().getExtras();
			File backupFile = (File) bundle.getSerializable("BACKUFILE");
			String contents[] = (String[]) bundle.getSerializable("CONTENTS");
			backupPath = backupFile.getPath();

			if (contents != null) {
				txtDate.setText("Backup date: " + contents[0]);
				txtDescriptions.setText("Descriptions: " + contents[1]);
			}
			if (backupFile != null) {
				txtFileName.setText("Backup file: " + backupFile.getName());
				ShowBackupTypeDialog();
			}
		} catch (Exception e) {
			Log.v("mediastore", "Error at RunRestoreActivity: "
					+ e.getMessage());
		}
	}

	@Override
	public void onBackPressed() {
		if(restoreTask!=null && !restoreTask.isFinished()){
			Toast.makeText(getApplicationContext(), "Please wait durring restore is running", Toast.LENGTH_LONG).show();
		}
		else super.onBackPressed();
	}

	private void RunRestoreImage() {
		String destinationPath =""; Environment.getExternalStorageDirectory()
				.toString();
//				+ "/mBackup";
		restoreTask = new RestoreImages(backupPath,
				destinationPath, txtStatus, txtPercent, progressBar,
				RestoreImages.BACKUP_MERGE);
		restoreTask.execute();
	}

	private void ShowBackupTypeDialog() {
		final CharSequence[] sources = new CharSequence[] { "Merge", "Override" };

		AlertDialog.Builder builder = new AlertDialog.Builder(
				RunRestoreActivity.this);
		builder.setTitle("Select restore type");

		// NategativeButton is on the right, so should be "Next"
		builder.setNegativeButton("Run", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				RunRestoreImage();
			}

		});

		builder.setPositiveButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
						RunRestoreActivity.this.finish();
					}
				});

		builder.setSingleChoiceItems(sources, 0,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						switch (item) {
						case 0:
							restoreType = RestoreImages.BACKUP_MERGE;
							break;
						case 1:
							restoreType = RestoreImages.BACKUP_OVERRIDE;
							break;

						default:
							break;
						}
					}
				}).show();
	}

}
